/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.uhk.ppro.response.web;

import cz.uhk.ppro.response.model.Answer;
import cz.uhk.ppro.response.model.Constants;
import cz.uhk.ppro.response.model.Question;
import cz.uhk.ppro.response.model.Test;
import cz.uhk.ppro.response.model.Types;
import cz.uhk.ppro.response.service.DatabaseService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Insidel
 */
@Controller
@SessionAttributes({"testData"})
public class TestController {

    @Autowired
    private DatabaseService databaseSession;

    @RequestMapping(value="tests_questions.do", method = RequestMethod.GET)
    protected String setupDruhyForm(
        @ModelAttribute(value="testData") Test test,
        BindingResult result,
        Model m)
    {
        int answerCount = 0;
        switch(test.getTypes().getId()) {
            case Constants.TEST_FORM_ABCD: answerCount = 4; break;
            case Constants.TEST_FORM_ANO_NE: answerCount = 2; break;
                
            default: new Exception("tento typ neni definovany");
        }
        
        
        List<Question> questions;
        questions = new ArrayList<Question>();
        for(int i = 1; i <= test.getNumber(); i++) {
            Question q = new Question();
            q.setAnswers(new ArrayList<Answer>());
            for(int j = 1; j <= answerCount; j++) {
                q.getAnswers().add(new Answer(false));
            }
            questions.add(q);
        }
        test.setQuestions(questions);

        return "formTestPartTwo";
    }

    @RequestMapping(value="test.do", method=RequestMethod.GET)
    protected String setupForm(@RequestParam(value="id",required=false) Integer id, Model m)
    {
        if (id!=null) {
            m.addAttribute("testData", databaseSession.getTestById(id));
        } else {

            m.addAttribute("testData", new Test());
        }

        return "formTest";
    }

    /**
     * Uloz prvni cast formulare
     *
     * @param id
     * @param command
     * @return
     */
    @RequestMapping(value="test.do", method=RequestMethod.POST)
    public String onSubmit(@RequestParam(value="id",required=false) Integer id, @ModelAttribute("testData") Test test)
    {
        if (id != null) {
            databaseSession.upravTest(test);
            return "redirect:/tests.do?id=" + id;
        } else {
            databaseSession.pridej(test);
            return "redirect:/tests_questions.do";
        }
    }

    /**
     * Uloz druhou cast formulare
     *
     * @return
     */
    @RequestMapping(value = "tests_questions.do", method = RequestMethod.POST)
    protected String submit(@ModelAttribute("testData") Test test, BindingResult result)
    {
        databaseSession.formUlozOtazkyOpdovedi(test);
        return "redirect:/tests.do";
    }

    @ModelAttribute("types")
    public List<Types> getTypes() {
        return databaseSession.getTypes();
    }

    @RequestMapping(value="tests.do")
    public ModelAndView zobrazTesty()
    {
        ModelAndView m = new ModelAndView("tests");
        m.addObject("all_tests", databaseSession.getTestsList());

        return m;
    }

    @RequestMapping(value="/smazTest", method=RequestMethod.POST)
    protected String smaz(@RequestParam(value = "id") Integer id)
    {
        Test t = databaseSession.getTestById(id);
        for( Question q: t.getQuestions()) {
            for( Answer a: databaseSession.getAnswers(q)) {
                databaseSession.odstranOdpoved(a.getId());
            }
            databaseSession.odstranOtazku(q.getId());
        }
        databaseSession.odstranTest(id);

        return "redirect:/tests.do";
    }


}
